package com.oocl.commandProces;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

public class CommandProcess {
	
	InputStream in;
	OutputStream out;
	
	public CommandProcess(InputStream in, OutputStream out){
		this.in = in;
		this.out = out;
	}

	public void view(String dir) throws IOException {
		File f = new File(dir);
		if(f.exists()){
			File[] files = f.listFiles();
			if(files.length == 0){
				out.write("directory is empty\r\n".getBytes());
				return;
			}else{
				for(File file:files){
					if(file.isDirectory()){
						view(file.getAbsolutePath());
					}else{
						out.write((file.getAbsolutePath()+"\r\n").getBytes());
					}
				}
			}
		}else{
			f.mkdirs();
		}
	}

	public void remove(String rmfile) throws IOException {
		File f = new File("D:\\scpSSh\\");
		String[] files = f.list();
		for(String file:files){
			if(rmfile.equals(file)){
				File des = new File("D:\\scpSSh\\"+file);
				des.delete();
				out.write("delete success!\r\n".getBytes());;
			}
		}
	}

	public void upload(String filename) throws IOException {
		BufferedReader reader=new BufferedReader(new InputStreamReader(in));
		OutputStream fout = new FileOutputStream("D:\\scpSSh\\"+filename);
		String signal = reader.readLine();
		while("200".equals(signal)){
			int temp = 0;
			byte[] bs = new byte[64];
			while((temp = in.read(bs))!=-1){
				fout.write(bs, 0, temp);
			}
			out.write("upload success!\r\n".getBytes());
		}
	}
}
